package com.shiningstars.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

import com.shiningstars.R;
import com.shiningstars.general.Config;
import com.shiningstars.general.Constants;
import com.shiningstars.general.Messages;
import com.shiningstars.general.Utilities;
import com.shiningstars.tools.ExceptionHandler;
import com.shiningstars.valuebeans.Feedback;

public class TeacherFeedback3Activity extends Activity implements Constants, Messages, Config {

	Feedback feedback;

	RadioGroup radioGroupMainTopic;
	RadioButton radioMainTopicShort;
	RadioButton radioMainTopicProper;
	RadioButton radioMainTopicLengthy;
	TextView textViewMainTopicResponse;
	RatingBar ratingBarMainTopicResponse;
	RadioGroup radioGroupMainTopicPpt;
	RadioButton radioMainTopicPptYes;
	RadioButton radioMainTopicPptNo;
	EditText editTextMainTopicComments;

	RadioGroup radioGroupSutra;
	RadioButton radioSutraShort;
	RadioButton radioSutraProper;
	RadioButton radioSutraLengthy;
	TextView textViewSutraResponse;
	RatingBar ratingBarSutraResponse;
	RadioGroup radioGroupSutraPpt;
	RadioButton radioSutraPptYes;
	RadioButton radioSutraPptNo;
	EditText editTextSutraComments;

	RadioGroup radioGroupStory;
	RadioButton radioStoryShort;
	RadioButton radioStoryProper;
	RadioButton radioStoryLengthy;
	TextView textViewStoryResponse;
	RatingBar ratingBarStoryResponse;
	RadioGroup radioGroupStoryPpt;
	RadioButton radioStoryPptYes;
	RadioButton radioStoryPptNo;
	EditText editTextStoryComments;

	RadioGroup radioGroupValue;
	RadioButton radioValueShort;
	RadioButton radioValueProper;
	RadioButton radioValueLengthy;
	TextView textViewValueResponse;
	RatingBar ratingBarValueResponse;
	RadioGroup radioGroupValuePpt;
	RadioButton radioValuePptYes;
	RadioButton radioValuePptNo;
	EditText editTextValueComments;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_teacher_feedback_3);

			Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
			SplashPageActivity.showSplash = false;

			getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

			radioGroupMainTopic = (RadioGroup) findViewById(R.id.tf3RadioGroup1);
			radioMainTopicShort = (RadioButton) findViewById(R.id.tf3RadioButton1);
			radioMainTopicProper = (RadioButton) findViewById(R.id.tf3RadioButton2);
			radioMainTopicLengthy = (RadioButton) findViewById(R.id.tf3RadioButton3);
			textViewMainTopicResponse = (TextView) findViewById(R.id.tf3TextView1);
			ratingBarMainTopicResponse = (RatingBar) findViewById(R.id.tf3RatingBar1);
			radioGroupMainTopicPpt = (RadioGroup) findViewById(R.id.tf3RadioGroup2);
			radioMainTopicPptYes = (RadioButton) findViewById(R.id.tf3RadioButton4);
			radioMainTopicPptNo = (RadioButton) findViewById(R.id.tf3RadioButton5);
			editTextMainTopicComments = (EditText) findViewById(R.id.tf3EditText1);

			radioGroupSutra = (RadioGroup) findViewById(R.id.tf3RadioGroup3);
			radioSutraShort = (RadioButton) findViewById(R.id.tf3RadioButton6);
			radioSutraProper = (RadioButton) findViewById(R.id.tf3RadioButton7);
			radioSutraLengthy = (RadioButton) findViewById(R.id.tf3RadioButton8);
			textViewSutraResponse = (TextView) findViewById(R.id.tf3TextView2);
			ratingBarSutraResponse = (RatingBar) findViewById(R.id.tf3RatingBar2);
			radioGroupSutraPpt = (RadioGroup) findViewById(R.id.tf3RadioGroup4);
			radioSutraPptYes = (RadioButton) findViewById(R.id.tf3RadioButton9);
			radioSutraPptNo = (RadioButton) findViewById(R.id.tf3RadioButton10);
			editTextSutraComments = (EditText) findViewById(R.id.tf3EditText2);

			radioGroupStory = (RadioGroup) findViewById(R.id.tf3RadioGroup5);
			radioStoryShort = (RadioButton) findViewById(R.id.tf3RadioButton11);
			radioStoryProper = (RadioButton) findViewById(R.id.tf3RadioButton12);
			radioStoryLengthy = (RadioButton) findViewById(R.id.tf3RadioButton13);
			textViewStoryResponse = (TextView) findViewById(R.id.tf3TextView3);
			ratingBarStoryResponse = (RatingBar) findViewById(R.id.tf3RatingBar3);
			radioGroupStoryPpt = (RadioGroup) findViewById(R.id.tf3RadioGroup6);
			radioStoryPptYes = (RadioButton) findViewById(R.id.tf3RadioButton14);
			radioStoryPptNo = (RadioButton) findViewById(R.id.tf3RadioButton15);
			editTextStoryComments = (EditText) findViewById(R.id.tf3EditText3);

			radioGroupValue = (RadioGroup) findViewById(R.id.tf3RadioGroup7);
			radioValueShort = (RadioButton) findViewById(R.id.tf3RadioButton16);
			radioValueProper = (RadioButton) findViewById(R.id.tf3RadioButton17);
			radioValueLengthy = (RadioButton) findViewById(R.id.tf3RadioButton18);
			textViewValueResponse = (TextView) findViewById(R.id.tf3TextView4);
			ratingBarValueResponse = (RatingBar) findViewById(R.id.tf3RatingBar4);
			radioGroupValuePpt = (RadioGroup) findViewById(R.id.tf3RadioGroup8);
			radioValuePptYes = (RadioButton) findViewById(R.id.tf3RadioButton19);
			radioValuePptNo = (RadioButton) findViewById(R.id.tf3RadioButton20);
			editTextValueComments = (EditText) findViewById(R.id.tf3EditText4);

			ratingBarMainTopicResponse.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

				@Override
				public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
					Utilities.setRatingDescription(textViewMainTopicResponse, rating);
				}
			});

			ratingBarSutraResponse.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

				@Override
				public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
					Utilities.setRatingDescription(textViewSutraResponse, rating);
				}
			});

			ratingBarStoryResponse.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

				@Override
				public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
					Utilities.setRatingDescription(textViewStoryResponse, rating);
				}
			});

			ratingBarValueResponse.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

				@Override
				public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
					Utilities.setRatingDescription(textViewValueResponse, rating);
				}
			});

			if (null != getIntent()) {
				if (null != getIntent().getExtras()) {
					if (null != getIntent().getExtras().get(FEEDBACK)) {
						feedback = (Feedback) getIntent().getExtras().get(FEEDBACK);

						if (SHORT.equals(feedback.getQuestion21())) {
							radioMainTopicShort.setChecked(true);
						} else if (PROPER.equals(feedback.getQuestion21())) {
							radioMainTopicProper.setChecked(true);
						} else if (LENGTHY.equals(feedback.getQuestion21())) {
							radioMainTopicLengthy.setChecked(true);
						}

						if (!Utilities.isBlankString(feedback.getQuestion22())) {
							float rating = Float.parseFloat(feedback.getQuestion22());
							ratingBarMainTopicResponse.setRating(rating);
						}

						if (YES.equals(feedback.getQuestion23())) {
							radioMainTopicPptYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion23())) {
							radioMainTopicPptNo.setChecked(true);
						}

						editTextMainTopicComments.setText(feedback.getQuestion24());

						if (SHORT.equals(feedback.getQuestion25())) {
							radioSutraShort.setChecked(true);
						} else if (PROPER.equals(feedback.getQuestion25())) {
							radioSutraProper.setChecked(true);
						} else if (LENGTHY.equals(feedback.getQuestion25())) {
							radioSutraLengthy.setChecked(true);
						}

						if (!Utilities.isBlankString(feedback.getQuestion26())) {
							float rating = Float.parseFloat(feedback.getQuestion26());
							ratingBarSutraResponse.setRating(rating);
						}

						if (YES.equals(feedback.getQuestion27())) {
							radioSutraPptYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion27())) {
							radioSutraPptNo.setChecked(true);
						}

						editTextSutraComments.setText(feedback.getQuestion28());

						if (SHORT.equals(feedback.getQuestion29())) {
							radioStoryShort.setChecked(true);
						} else if (PROPER.equals(feedback.getQuestion29())) {
							radioStoryProper.setChecked(true);
						} else if (LENGTHY.equals(feedback.getQuestion29())) {
							radioStoryLengthy.setChecked(true);
						}

						if (!Utilities.isBlankString(feedback.getQuestion30())) {
							float rating = Float.parseFloat(feedback.getQuestion30());
							ratingBarStoryResponse.setRating(rating);
						}

						if (YES.equals(feedback.getQuestion31())) {
							radioStoryPptYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion31())) {
							radioStoryPptNo.setChecked(true);
						}

						editTextStoryComments.setText(feedback.getQuestion32());

						if (SHORT.equals(feedback.getQuestion33())) {
							radioValueShort.setChecked(true);
						} else if (PROPER.equals(feedback.getQuestion33())) {
							radioValueProper.setChecked(true);
						} else if (LENGTHY.equals(feedback.getQuestion33())) {
							radioValueLengthy.setChecked(true);
						}

						if (!Utilities.isBlankString(feedback.getQuestion34())) {
							float rating = Float.parseFloat(feedback.getQuestion34());
							ratingBarValueResponse.setRating(rating);
						}

						if (YES.equals(feedback.getQuestion35())) {
							radioValuePptYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion35())) {
							radioValuePptNo.setChecked(true);
						}

						editTextValueComments.setText(feedback.getQuestion36());
					}
				}
			}
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), TeacherFeedback3Activity.class.getName(), e);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		try {
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.feedback_menu, menu);
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), TeacherFeedback3Activity.class.getName(), e);
		}

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		try {
			if (validate()) {
				Intent i = null;

				if (BACK.equals(item.getTitle())) {
					i = new Intent(TeacherFeedback3Activity.this, TeacherFeedback2Activity.class);
				} else if (NEXT.equals(item.getTitle())) {
					i = new Intent(TeacherFeedback3Activity.this, TeacherFeedback4Activity.class);
				}

				i.putExtra(FEEDBACK, feedback);
				startActivity(i);
				finish();
			}
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), TeacherFeedback3Activity.class.getName(), e);
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		try {
			AlertDialog.Builder builder = new AlertDialog.Builder(TeacherFeedback3Activity.this);

			builder.setMessage(FEEDBACK_CANCEL);
			builder.setCancelable(false);
			builder.setPositiveButton(OK, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Intent i = null;

					String currentRole = Utilities.getSharedPreference(getApplicationContext(), CURRENT_ROLE);
					if (ADMIN.equals(currentRole)) {
						i = new Intent(TeacherFeedback3Activity.this, AdminActivity.class);
					} else if (TEACHER.equals(currentRole)) {
						i = new Intent(TeacherFeedback3Activity.this, MyFeedbackActivity.class);
					}

					startActivity(i);
					finish();
				}
			});
			builder.setNegativeButton(CANCEL, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {

				}
			});

			AlertDialog alert = builder.create();
			alert.show();
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), TeacherFeedback3Activity.class.getName(), e);
		}
	}

	private boolean validate() {
		boolean isValid = true;
		String errorMessage = null;

		int radioGroupMainTopicId = radioGroupMainTopic.getCheckedRadioButtonId();
		if (isValid && radioGroupMainTopicId == -1) {
			isValid = false;
			errorMessage = INVALID_MAIN_TOPIC_CONTENT;
			radioGroupMainTopic.requestFocus();
		} else {
			if (radioGroupMainTopicId == radioMainTopicShort.getId()) {
				feedback.setQuestion21(radioMainTopicShort.getText().toString());
			} else if (radioGroupMainTopicId == radioMainTopicProper.getId()) {
				feedback.setQuestion21(radioMainTopicProper.getText().toString());
			} else if (radioGroupMainTopicId == radioMainTopicLengthy.getId()) {
				feedback.setQuestion21(radioMainTopicLengthy.getText().toString());
			}
		}

		if (isValid && ratingBarMainTopicResponse.getRating() == 0) {
			isValid = false;
			errorMessage = INVALID_MAIN_TOPIC_RESPONSE;
			ratingBarMainTopicResponse.requestFocus();
		} else {
			feedback.setQuestion22(String.valueOf(ratingBarMainTopicResponse.getRating()));
		}

		int radioGroupMainTopicPptId = radioGroupMainTopicPpt.getCheckedRadioButtonId();
		if (isValid && radioGroupMainTopicPptId == -1) {
			isValid = false;
			errorMessage = INVALID_MAIN_TOPIC_SYNC;
			radioGroupMainTopicPpt.requestFocus();
		} else {
			if (radioGroupMainTopicPptId == radioMainTopicPptYes.getId()) {
				feedback.setQuestion23(radioMainTopicPptYes.getText().toString());
			} else if (radioGroupMainTopicPptId == radioMainTopicPptNo.getId()) {
				feedback.setQuestion23(radioMainTopicPptNo.getText().toString());
			}
		}

		if (isValid && Utilities.isBlankString(editTextMainTopicComments.getText().toString())) {
			isValid = false;
			errorMessage = INVALID_MAIN_TOPIC_COMMENTS;
			editTextMainTopicComments.requestFocus();
		} else {
			feedback.setQuestion24(editTextMainTopicComments.getText().toString());
		}

		int radioGroupSutraId = radioGroupSutra.getCheckedRadioButtonId();
		if (isValid && radioGroupSutraId == -1) {
			isValid = false;
			errorMessage = INVALID_SUTRA_CONTENT;
			radioGroupSutra.requestFocus();
		} else {
			if (radioGroupSutraId == radioSutraShort.getId()) {
				feedback.setQuestion25(radioSutraShort.getText().toString());
			} else if (radioGroupSutraId == radioSutraProper.getId()) {
				feedback.setQuestion25(radioSutraProper.getText().toString());
			} else if (radioGroupSutraId == radioSutraLengthy.getId()) {
				feedback.setQuestion25(radioSutraLengthy.getText().toString());
			}
		}

		if (isValid && ratingBarSutraResponse.getRating() == 0) {
			isValid = false;
			errorMessage = INVALID_SUTRA_RESPONSE;
			ratingBarSutraResponse.requestFocus();
		} else {
			feedback.setQuestion26(String.valueOf(ratingBarSutraResponse.getRating()));
		}

		int radioGroupSutraPptId = radioGroupSutraPpt.getCheckedRadioButtonId();
		if (isValid && radioGroupSutraPptId == -1) {
			isValid = false;
			errorMessage = INVALID_SUTRA_SYNC;
			radioGroupSutraPpt.requestFocus();
		} else {
			if (radioGroupSutraPptId == radioSutraPptYes.getId()) {
				feedback.setQuestion27(radioSutraPptYes.getText().toString());
			} else if (radioGroupSutraPptId == radioSutraPptNo.getId()) {
				feedback.setQuestion27(radioSutraPptNo.getText().toString());
			}
		}

		if (isValid && Utilities.isBlankString(editTextSutraComments.getText().toString())) {
			isValid = false;
			errorMessage = INVALID_SUTRA_COMMENTS;
			editTextSutraComments.requestFocus();
		} else {
			feedback.setQuestion28(editTextSutraComments.getText().toString());
		}

		int radioGroupStoryId = radioGroupStory.getCheckedRadioButtonId();
		if (isValid && radioGroupStoryId == -1) {
			isValid = false;
			errorMessage = INVALID_STORY_CONTENT;
			radioGroupStory.requestFocus();
		} else {
			if (radioGroupStoryId == radioStoryShort.getId()) {
				feedback.setQuestion29(radioStoryShort.getText().toString());
			} else if (radioGroupStoryId == radioStoryProper.getId()) {
				feedback.setQuestion29(radioStoryProper.getText().toString());
			} else if (radioGroupStoryId == radioStoryLengthy.getId()) {
				feedback.setQuestion29(radioStoryLengthy.getText().toString());
			}
		}

		if (isValid && ratingBarStoryResponse.getRating() == 0) {
			isValid = false;
			errorMessage = INVALID_STORY_RESPONSE;
			ratingBarStoryResponse.requestFocus();
		} else {
			feedback.setQuestion30(String.valueOf(ratingBarStoryResponse.getRating()));
		}

		int radioGroupStoryPptId = radioGroupStoryPpt.getCheckedRadioButtonId();
		if (isValid && radioGroupStoryPptId == -1) {
			isValid = false;
			errorMessage = INVALID_STORY_SYNC;
			radioGroupStoryPpt.requestFocus();
		} else {
			if (radioGroupStoryPptId == radioStoryPptYes.getId()) {
				feedback.setQuestion31(radioStoryPptYes.getText().toString());
			} else if (radioGroupStoryPptId == radioStoryPptNo.getId()) {
				feedback.setQuestion31(radioStoryPptNo.getText().toString());
			}
		}

		if (isValid && Utilities.isBlankString(editTextStoryComments.getText().toString())) {
			isValid = false;
			errorMessage = INVALID_STORY_COMMENTS;
			editTextStoryComments.requestFocus();
		} else {
			feedback.setQuestion32(editTextStoryComments.getText().toString());
		}

		int radioGroupValueId = radioGroupValue.getCheckedRadioButtonId();
		if (isValid && radioGroupValueId == -1) {
			isValid = false;
			errorMessage = INVALID_VALUE_CONTENT;
			radioGroupValue.requestFocus();
		} else {
			if (radioGroupValueId == radioValueShort.getId()) {
				feedback.setQuestion33(radioValueShort.getText().toString());
			} else if (radioGroupValueId == radioValueProper.getId()) {
				feedback.setQuestion33(radioValueProper.getText().toString());
			} else if (radioGroupValueId == radioValueLengthy.getId()) {
				feedback.setQuestion33(radioValueLengthy.getText().toString());
			}
		}

		if (isValid && ratingBarValueResponse.getRating() == 0) {
			isValid = false;
			errorMessage = INVALID_VALUE_RESPONSE;
			ratingBarValueResponse.requestFocus();
		} else {
			feedback.setQuestion34(String.valueOf(ratingBarValueResponse.getRating()));
		}

		int radioGroupValuePptId = radioGroupValuePpt.getCheckedRadioButtonId();
		if (isValid && radioGroupValuePptId == -1) {
			isValid = false;
			errorMessage = INVALID_VALUE_SYNC;
			radioGroupValuePpt.requestFocus();
		} else {
			if (radioGroupValuePptId == radioValuePptYes.getId()) {
				feedback.setQuestion35(radioValuePptYes.getText().toString());
			} else if (radioGroupValuePptId == radioValuePptNo.getId()) {
				feedback.setQuestion35(radioValuePptNo.getText().toString());
			}
		}

		if (isValid && Utilities.isBlankString(editTextValueComments.getText().toString())) {
			isValid = false;
			errorMessage = INVALID_VALUE_COMMENTS;
			editTextValueComments.requestFocus();
		} else {
			feedback.setQuestion36(editTextValueComments.getText().toString());
		}

		if (!isValid) {
			AlertDialog.Builder builder = new AlertDialog.Builder(TeacherFeedback3Activity.this);

			builder.setMessage(errorMessage);
			builder.setCancelable(false);
			builder.setTitle(ERROR);
			builder.setPositiveButton(OK, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
				}
			});

			AlertDialog alert = builder.create();
			alert.show();
		}

		return isValid;
	}

	@Override
	protected void onPause() {
		super.onPause();
		SplashPageActivity.showSplash = true;
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (SplashPageActivity.showSplash) {
			LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
			View popupView = layoutInflater.inflate(R.layout.activity_splash_page, null);
			final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT, true);

			findViewById(R.id.tf3TextView1).post(new Runnable() {
				public void run() {
					popupWindow.showAtLocation(findViewById(R.id.tf3TextView1), Gravity.CENTER, 0, 0);
				}
			});

			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					if (null != popupWindow && popupWindow.isShowing()) {
						popupWindow.dismiss();
					}
				}
			}, SPLASH_TIME_OUT);

			SplashPageActivity.showSplash = false;
		}
	}
}